package com.zzt.springbootdemo.controller;

import com.zzt.springbootdemo.model.MessageInfo;
import com.zzt.springbootdemo.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description
 * User: panda
 * Date: 2025-02-20
 * Time: 20:26
 */
@RequestMapping("/message")
@RestController
public class MessageController {

    @Autowired
    private MessageService messageService;

//    private List<MessageInfo> messageInfoList = new ArrayList<>();

//    @PostMapping(value = "/publish", produces = "application/json")
    @PostMapping(value = "/publish")
    public String publish(@RequestBody MessageInfo messageInfo){
        if(!StringUtils.hasLength(messageInfo.getFrom())
                || !StringUtils.hasLength(messageInfo.getTo())
                || !StringUtils.hasLength(messageInfo.getMessage())){
            return "{\"ok\": 0}";
        }

        //存储留言
        messageService.addMessage(messageInfo);
//        messageInfoList.add(messageInfo);
        return "{\"ok\": 1}";
    }

    @GetMapping("/getList")
    public List<MessageInfo> getList(){
        return messageService.queryAll();
    }
}
